/*
Disclaimer of Warranty: THE PACKAGE IS PROVIDED
BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS'
AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT
ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW.
UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF
THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


(c) Copyright 2013 Edward Principe
All Rights Reserved

*/
#include "stdafx.h"
#include "parseall.h"

////////////////////////////////////////////////////////////
// Copy: Copies class
///////////////////////////////////////////////////////////
void ParseAll::Copy(const ParseAll& rhs)
{
	if (this == &rhs)
		return;

	// Erase current words
	_list_words.erase(_list_words.begin(), _list_words.end());

	// Copy words
	for (list<ParseNode>::const_iterator i = rhs._list_words.begin(); i != rhs._list_words.end(); i++)
	{
		_list_words.push_back(static_cast<ParseNode>(*i));
	}

	// Set iterator to the beginning
	_i = _list_words.begin();
}

////////////////////////////////////////////////////////////
// InitBuffer: Initializes buffer
/////////////////////////////////////////////////////////////
void ParseAll::InitBuffer(const wchar_t *buffer)
{
	// Locals
	SmtPtr<ParseNode> pn_begin, parseNode, pn_end; // For the word list
	TokErr err;

	// Erase words
	_list_words.erase(_list_words.begin(), _list_words.end());

	// Set beginning
	pn_begin->is_begin = true;

	_list_words.push_back(*pn_begin);

	// Initilize buffer
	_parse->InitBuffer(buffer);

	// Parse buffer
	while (true)
	{
		// Parse next word
		err = _parse->Next();

		// At end?
		if (_parse->IsEnd() && _parse->GetWord() == L"")
			break;

		// Fill out the node
		parseNode->err = err;
		parseNode->word = _parse->GetWord();
		parseNode->is_string = _parse->IsString();

		// Add to the end of the list
		_list_words.push_back(*parseNode);

		// At end?
		if (_parse->IsEnd())
			break;

	}

	// Set the is_end flag
	pn_end->is_end = true;
	_list_words.push_back(*pn_end);

	// Set iterator to the beginning
	_i = _list_words.begin();
}

//////////////////////////////////////////////////////////////////////////
// Prev: Goes back a word
//////////////////////////////////////////////////////////////////////////
TokErr ParseAll::Prev()
{
	// At beginning?
	if (_i->is_begin)
		return _i->err;

	// Go to previous word
	_i--;

	// Return error
	return _i->err;
}

////////////////////////////////////////////////////////////////////////
// Next: Goes foward a word
///////////////////////////////////////////////////////////////////////
TokErr ParseAll::Next()
{
	// At end?
	if (_i->is_end)
		return _i->err;

	// Get next word
	_i++;

	// Return error
	return _i->err;
}

////////////////////////////////////////////////////////////////////////
// Reset: Resets the class
///////////////////////////////////////////////////////////////////////
void ParseAll::Reset()
{
	_list_words.erase(_list_words.begin(), _list_words.end());
}